package com.by.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.by.dto.SysRoleQueryParam;
import com.by.dto.SysUserAddOrUpdateParam;
import com.by.entity.SysRole;
import com.by.entity.SysUser;
import com.by.model.Result;
import com.by.service.SysRoleService;
import com.by.service.SysUserService;
import com.by.util.AuthUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author ServivE
 * @version 0.1
 * @className SysUserController
 * @description
 * @date 2024/12/10 16:52
 * @since jdk17
 */
@Api(tags = "管理员接口管理")
@RestController
@RequestMapping("/sys/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;



    @ApiOperation("根据用户id查询用户信息")
    @GetMapping("/info")
    public Result<SysUser> getInfoByUserId() {
        //获取用户id
        Long loginUserId = AuthUtils.getLoginUserId();
        //根据id查询用户信息
        SysUser sysUser = sysUserService.getById(loginUserId);
        return Result.success(sysUser);
    }
    @ApiOperation("多条件分页查询管理员列表")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('sys:user:page')")//在方法执行之前，会进行访问控制的检查，只有符合权限要求的用户才能够执行该方法
    public Result<Page<SysUser>> loadSysRolePage(Page<SysUser> page, SysRoleQueryParam queryParam) {
        page = sysUserService.page(page , new LambdaQueryWrapper<SysUser>()
                .like(StringUtils.hasText(queryParam.getUsername()), SysUser::getUsername,queryParam.getUsername())
                .orderByDesc(SysUser::getCreateTime)
        );
        return Result.success(page);
    }

    @ApiOperation("新增管理员")
    @PostMapping("save")
    @PreAuthorize("hasAuthority('sys:user:save')")
    public Result<String> saveSysUser(@RequestBody SysUserAddOrUpdateParam addOrUpdateParam) {
        Integer i = sysUserService.addSysUser(addOrUpdateParam);
        return Result.handle(i>0);
    }
}
